<?php
# ------------------------------------------------
# License and copyright:
#
# This program is designed and copyrighted by
# Aaron Colman on Feb 27, 2004 under the terms 
# of the GPL provided under gpl.txt
# Edited by Peter Drake 2004
#
# ------------------------------------------------

// TODO: This need to be moved to a module!
/*

$mail_server_string = "{".$cfg['helpdesk_POP_host'].":".$cfg['helpdesk_POP_port']."/".$cfg['helpdesk_mailtype']."}".$cfg['helpdesk_POP_mailbox'];
// Check if PHP has IMAP support first!
if (function_exists('imap_open')) {

	if ($conn = @imap_open($mail_server_string, $cfg['helpdesk_POP_un'], $cfg['helpdesk_POP_pass'])) {

	$headers = 0;
	if ($headers = imap_headers($conn)) {
		$Num_Emails = sizeof($headers);
	} else {
		$Num_Emails = 0;
	}

	for($i = 1; $i <= $Num_Emails; $i++) {
		$mailHeader = imap_headerinfo($conn, $i);
		$ticket_headers = imap_fetchbody($conn, $i, 0);
		$ticket_body = imap_fetchbody($conn, $i, 1);
		$ticket_body2 = imap_fetchbody($conn, $i, 2);
		$ticket_subject = $mailHeader->subject;
		$ticket_date = date("Y-m-d h:i:s", strtotime($mailHeader->date));
	
		// Remove quoted sections from the body of the email
		$body_replace[] = "/\n([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)).*\n>.*\r/";
		$body_replace[] = "/\n>.*\r/";
		$ticket_body = preg_replace($body_replace, "", $ticket_body);

		$from = $mailHeader->from;
		foreach ($from as $id => $object) {
			$fromname = $object->personal;
			$ticket_address = $object->mailbox . "@" . $object->host;
			$fromhost = $object->host;
		}

		$fromname = preg_replace("/\{.*\}/i", "", $fromname);
		$fromname = preg_replace("/\(.*\)/i", "", $fromname);
		$fromname = preg_replace("/\[.*\]/i", "", $fromname);
		$fromname = preg_replace("/<.*>/i", "", $fromname);

		$IsEmail = eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$",$fromname);
		if ($IsEmail == 1) {
			$FirstName = $fromname;
			$LastName = '';
		} else {
			$Comma_List=explode(',',trim($fromname));
			$Comma_MaxIndex = sizeof($Comma_List);
			if ($Comma_MaxIndex > 1) {
				$FirstName = '';
				$LastName = $Comma_List[0];
				for ($j=1; $j<=$Comma_MaxIndex-1; $j++) {
					$FirstName .= ' ';
					$FirstName .= $Comma_List[$j];
				}
				if ($Concat_FirstWithMiddle != 1) {
					$Space_List = explode(' ',trim($FirstName));
					$FirstName = $Space_List[0];
				}
			} else {
				$Space_List=explode(' ',trim($fromname));
				$Space_MaxIndex = sizeof($Space_List);
				$LastName  = $Space_List[$Space_MaxIndex-1];
				$FirstName = $Space_List[0];
			}
		}

		$FirstName = trim($FirstName);
		$LastName = trim($LastName);

		preg_match_all("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/",$ticket_headers,$ips);
		$ticket_IP = $ips[0][0];

		preg_match_all("/TN[0-9]{9}/",$ticket_subject,$tickets);
		$ticket_number = str_replace("TN", "", $tickets[0][0]);
		if ($ticket_number != "") {
			$ticket_subject = preg_replace("/TN[0-9]{9}/","",$ticket_subject);
		}
	
		preg_match("/Importance: (.*)\b/",$ticket_headers,$importance_array);
		$importance = $importance_array[1];
		preg_match("/X-Priority: ([0-9]{1})/",$ticket_headers,$priority_array);
		$priority = intval($priority_array[1]);

		$pos = strpos($ticket_subject, $cfg['helpdesk_spam']);
		if ($pos === false) { 
			$spam_filtered = 0; 
		} else { 
			$spam_filtered = 1;
		}
	
		if (stristr($importance, "high") || $priority < 3) {
			$priorityid = 3;
		} elseif (stristr($importance, "medium") || $priority == 3) {
			$priorityid = 1;
		} elseif (stristr($importance, "low") || $priority > 3) {
			$priorityid = 2;
		}
	
		if ($ticket_number != "") {
			$query = "SELECT id from helpdesk_tickets WHERE ticketid = '$ticket_number'";
			$res = mysql_query($query);
			if ($row = mysql_fetch_array($res)) {
				if (updateticketfromemail($row["id"], $FirstName, $LastName, $ticket_address, $ticket_subject, $ticket_body, $ticket_IP, $fromhost, $priorityid, $ticket_date)) {
					imap_delete($conn, $i);
				}
			} else {
				if (createticketfromemail($FirstName, $LastName, $ticket_address, $ticket_subject, $ticket_body, $ticket_IP, $fromhost, $priorityid, $ticket_date)) {
					imap_delete($conn, $i);
				}
			}
		} else {
			if (createticketfromemail($FirstName, $LastName, $ticket_address, $ticket_subject, $ticket_body, $ticket_IP, $fromhost, $priorityid, $ticket_date)) {
				imap_delete($conn, $i);
			}
		}	

	}
	imap_expunge($conn);
	imap_close($conn);

	} else {
		webcp_log(1,0,"tt","ERROR: could not connect to host: $mail_server_string",0);
	}

} else {
	// NO IMAP Support
	//webcp_log(1,0,"tt","ERROR: No IMAP Support - Cannot check for tickets",0);
}
*/
?>
